import { axiosError, defaultErrorMessage } from 'src/util/errorHandler';
import http from 'src/util/http';

import qs from 'qs'

type PostAuthSigninRequest = any
type PostAuthSigninResponse = any



const signup = async (dataParam: PostAuthSigninRequest) => {
  const data = qs.stringify(dataParam)
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/UserRegister`, { data });
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};



const signin = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/UserLogin?uName=${data.email}&uPwd=${data.password}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

const signout = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<void, PostAuthSigninRequest>(`User/LogOut?uName=${data.uMail}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};


const emailResend = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/getSendMail?uMail=${data.uMail}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

const emailVerify = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/getMailVerify?uMail=${data.uMail}&uCode=${data.uCode}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

const editName = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/editName?uMail=${data.uMail}&uName=${data.uName}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};
const editPwd = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/editPwd?uMail=${data.uMail}&uPwd=${data.uPwd}&newPwd=${data.newPwd}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};
const UserGoogleYz = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/UserGoogleYz?uName=${data.uName}&uGoogle=${data.uGoogle}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

const getGoogleVerify = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/getGoogleVerify?uMail=${data.uMail}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

const GoogleVerify = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/GoogleVerify?uMail=${data.uMail}&gCode=${data.gCode}&gState=${data.gState}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

const getResetPwd = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/getResetPwd?uMail=${data.uMail}&uCode=${data.uCode}&newPwd=${data.newPwd}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};
// 读取银行列表getMyBank用户ID[guid]用户授权后的token值
const getMyBank = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/getMyBank?FUserID=${data.FUserID}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

// 新增银行帐户
const addMyBank = async (dataParam: PostAuthSigninRequest) => {
  const data = qs.stringify(dataParam)
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/AddMyBank`, { data });
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};


// 删除用户银行DelMyBankFIDtoken值
const delMyBank = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`User/DelMyBank?FID=${data.FID}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};
export default {
  signup,
  signin,
  signout,
  emailResend,
  emailVerify,
  editName,
  editPwd,
  getGoogleVerify,
  GoogleVerify,
  UserGoogleYz,
  getResetPwd,
  getMyBank,
  addMyBank,
  delMyBank,
};
